<?php
    if(!function_exists('parse_padding')){
        function parse_padding($source)
        {
            $length  = strlen(strval(count($source['source']) + $source['first']));
            return 40 + ($length - 1) * 8;
        }
    }

    if(!function_exists('parse_class')){
        function parse_class($name)
        {
            $names = explode('\\', $name);
            return '<abbr title="'.$name.'">'.end($names).'</abbr>';
        }
    }

    if(!function_exists('parse_file')){
        function parse_file($file, $line)
        {
            return '<a class="toggle" title="'."{$file} line {$line}".'">'.basename($file)." line {$line}".'</a>';
        }
    }

    if(!function_exists('parse_args')){
        function parse_args($args)
        {
            $result = [];

            foreach ($args as $key => $item) {
                switch (true) {
                    case is_object($item):
                        $value = sprintf('<em>object</em>(%s)', parse_class(get_class($item)));
                        break;
                    case is_array($item):
                        if(count($item) > 3){
                            $value = sprintf('[%s, ...]', parse_args(array_slice($item, 0, 3)));
                        } else {
                            $value = sprintf('[%s]', parse_args($item));
                        }
                        break;
                    case is_string($item):
                        if(strlen($item) > 20){
                            $value = sprintf(
                                '\'<a class="toggle" title="%s">%s...</a>\'',
                                htmlentities($item),
                                htmlentities(substr($item, 0, 20))
                            );
                        } else {
                            $value = sprintf("'%s'", htmlentities($item));
                        }
                        break;
                    case is_int($item):
                    case is_float($item):
                        $value = $item;
                        break;
                    case is_null($item):
                        $value = '<em>null</em>';
                        break;
                    case is_bool($item):
                        $value = '<em>' . ($item ? 'true' : 'false') . '</em>';
                        break;
                    case is_resource($item):
                        $value = '<em>resource</em>';
                        break;
                    default:
                        $value = htmlentities(str_replace("\n", '', var_export(strval($item), true)));
                        break;
                }

                $result[] = is_int($key) ? $value : "'{$key}' => {$value}";
            }

            return implode(', ', $result);
        }
    }
?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>系统发生错误</title>
    <meta name="robots" content="noindex,nofollow" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <style>
        body,
        h1,
        h2,
        h3,
        h4,
        h5,
        h6,
        dl,
        dt,
        dd,
        input,
        form,
        span,
        img,
        p,
        ul,
        li,
        margin,
        padding,
        div {margin: 0px;padding: 0px;font-size: 12px;font-family: Arial, Helvetica, sans-serif;font-weight: 400;}
        img,border {border: 0;}
        a {color: #666666;text-decoration: none;}
        body {	padding: 0px;margin: 0px;width: 100%;}
        .c:before,
        .c:after {display: table;content: " ";clear: both;}

        img {
            vertical-align: top;
            margin: 0px;
            padding: 0px;
            max-width: 100%;
        }
        * {
            -moz-box-sizing: border-box;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
        }

        .baocuo-img {width: 425px;height: 420px; margin:0px auto;text-align:center; margin-top: 80px;}
        .baocuo-p {width:60%;margin:0px auto; margin-top:30px;}
        .baocuo-p p { font-size:22px; color:#43c6fe; text-align:left;}
        .baocuo-p span {font-size:16px;text-align:center;}
        .baocuo-p span a { margin: 0px auto;margin-top: 20px; width:100px; height:40px; background:#46a8cd; border-radius:5px; line-height:40px; text-decoration:none; color:#ffffff;display: block;}
        .baocuo-p span a:hover {color:#ffffff; }

        .baocuo-img {width: 350px;height: 350px; margin:0px auto;text-align:center; margin-top: 80px;}

        .content { padding-top: 10px;width: 60%;margin: 0px auto;}
        .info {margin-bottom: 12px;}
        .info .title {margin-bottom: 3px;background:#eee;padding:10px;}
        .info .title h3 {  color: #000; font-weight: 700; font-size: 16px;}
        .info .text {line-height: 24px;padding:0 10px;}
        .info .text p {font-size:14px; color:#333333;border-bottom:#ccc 1px solid;}
        .info { margin-bottom: 12px;}
        .info .title { margin-bottom: 3px;}
        .info .title h3 {color: #000;  font-weight: 700;font-size: 16px;}
        .copyright { color: #666666;}
        .copyright a {color: #000; text-decoration: none;}


        @media (min-width: 1200px) and (max-width: 1385px) {
            .baocuo-img {width: 350px;height: 350px; margin:0px auto;text-align:center; margin-top: 80px;}
            .baocuo-p{width: 60%;}
            .content { padding-top: 10px;width: 60%;margin: 0px auto;}
            .info {margin-bottom: 12px;}
            .info .title {margin-bottom: 3px;}
            .info .title h3 {  color: #000; font-weight: 700; font-size: 16px;}
            .info .text {line-height: 24px;}
            .info .text p {font-size:14px; color:#333333;}
            .info { margin-bottom: 12px;}
            .info .title { margin-bottom: 3px;}
            .info .title h3 {color: #000;  font-weight: 700;font-size: 16px;}
            .copyright { color: #666666;}
            .copyright a {color: #000; text-decoration: none;}
        }
        @media (min-width: 992px) and (max-width: 1200px) {
            .baocuo-img {width: 330px;height: 330px; margin:0px auto;text-align:center;  margin-top:60px; }
            .baocuo-p{width: 60%;}
            .content { padding-top: 10px;width: 60%;margin: 0px auto;}
            .info {margin-bottom: 12px;}
            .info .title {margin-bottom: 3px;}
            .info .title h3 {  color: #000; font-weight: 700; font-size: 16px;}
            .info .text {line-height: 24px;}
            .info .text p {font-size:14px; color:#333333;}
            .info { margin-bottom: 12px;}
            .info .title { margin-bottom: 3px;}
            .info .title h3 {color: #000;  font-weight: 700;font-size: 16px;}
            .copyright { color: #666666;}
            .copyright a {color: #000; text-decoration: none;}
        }
        @media (min-width: 768px) and (max-width: 991px){
            .baocuo-img {width: 300px;height: 300px; margin:0px auto;text-align:center;  margin-top:60px; }
            .baocuo-p{width: 70%;}
            .content { padding-top: 10px;width: 70%;margin: 0px auto;}
            .info {margin-bottom: 12px;}
            .info .title {margin-bottom: 3px;}
            .info .title h3 {  color: #000; font-weight: 700; font-size: 16px;}
            .info .text {line-height: 24px;}
            .info .text p {font-size:14px; color:#333333;}
            .info { margin-bottom: 12px;}
            .info .title { margin-bottom: 3px;}
            .info .title h3 {color: #000;  font-weight: 700;font-size: 16px;}
            .copyright { color: #666666;}
            .copyright a {color: #000; text-decoration: none;}
        }
        @media (max-width:768px) {

            .baocuo-img {width:280px; height: 280px; margin:0px auto;text-align:center;  margin-top:80px; }
            .baocuo-p{width: 80%;}
            .content { padding-top: 10px;width: 80%;margin: 0px auto;}
            .info {margin-bottom: 12px;}
            .info .title {margin-bottom: 3px;}
            .info .title h3 {  color: #000; font-weight: 700; font-size: 16px;}
            .info .text {line-height: 24px;}
            .info .text p {font-size:14px; color:#333333;}
            .info { margin-bottom: 12px;}
            .info .title { margin-bottom: 3px;}
            .info .title h3 {color: #000;  font-weight: 700;font-size: 16px;}
            .copyright { color: #666666;}
            .copyright a {color: #000; text-decoration: none;}
        }
        @media(max-width: 504px){

            .baocuo-img {width:250px; height: 250px; margin:0px auto;text-align:center;  margin-top:60px; }
            .baocuo-p {width:90%;margin:0px auto; margin-top:25px;}
            .baocuo-p p {font-size: 20px; color:#43c6fe; text-align:left;}
            .content { padding-top: 10px;width: 90%;margin: 0px auto;}
            .info {margin-bottom: 12px;}
            .info .title {margin-bottom: 3px;}
            .info .title h3 {  color: #000; font-weight: 700; font-size: 16px;}
            .info .text {line-height: 24px;}
            .info .text p {font-size:14px; color:#333333;}
            .info { margin-bottom: 12px;}
            .info .title { margin-bottom: 3px;}
            .info .title h3 {color: #000;  font-weight: 700;font-size: 16px;}
            .copyright { color: #666666;}
            .copyright a {color: #000; text-decoration: none;}
        }
        /* Layout */
        .col-md-3 {
            width: 25%;
        }
        .col-md-9 {
            width: 75%;
        }
        [class^="col-md-"] {
            float: left;
        }
        .clearfix {
            clear:both;
        }
        @media only screen
        and (min-device-width : 375px)
        and (max-device-width : 667px) {
            .col-md-3,
            .col-md-9 {
                width: 100%;
            }
        }
        /* Exception Variables */
        .exception-var table{
            width: 100%;
            margin: 12px 0;
            box-sizing: border-box;
            table-layout:fixed;
            word-wrap:break-word;
        }
        .exception-var table caption{
            text-align: left;
            font-size: 16px;
            font-weight: bold;
            padding: 6px 0;
        }
        .exception-var table caption small{
            font-weight: 300;
            display: inline-block;
            margin-left: 10px;
            color: #ccc;
        }
        .exception-var table tbody{
            font-size: 13px;
            font-family: Consolas,"Liberation Mono",Courier,"微软雅黑";
        }
        .exception-var table td{
            padding: 0 6px;
            vertical-align: top;
            word-break: break-all;
        }
        .exception-var table td:first-child{
            width: 28%;
            font-weight: bold;
            white-space: nowrap;
        }
        .exception-var table td pre{
            margin: 0;
        }
        /* SPAN elements with the classes below are added by prettyprint. */
        pre.prettyprint .pln { color: #000 }  /* plain text */
        pre.prettyprint .str { color: #080 }  /* string content */
        pre.prettyprint .kwd { color: #008 }  /* a keyword */
        pre.prettyprint .com { color: #800 }  /* a comment */
        pre.prettyprint .typ { color: #606 }  /* a type name */
        pre.prettyprint .lit { color: #066 }  /* a literal value */
        /* punctuation, lisp open bracket, lisp close bracket */
        pre.prettyprint .pun, pre.prettyprint .opn, pre.prettyprint .clo { color: #660 }
        pre.prettyprint .tag { color: #008 }  /* a markup tag name */
        pre.prettyprint .atn { color: #606 }  /* a markup attribute name */
        pre.prettyprint .atv { color: #080 }  /* a markup attribute value */
        pre.prettyprint .dec, pre.prettyprint .var { color: #606 }  /* a declaration; a variable name */
        pre.prettyprint .fun { color: red }  /* a function name */
        .line-error span{ color:red }
    </style>
</head>
<body>
<div class="baocuo-box">
    <div class="baocuo-img"><img src="<?php echo url('/','',false,true); ?>plugins/qbt_exception_page/images/edf.png"></div>
    <div class="baocuo-p">
        <p><?php echo htmlentities($message); ?></p>
        <span><a href="<?php echo url('/','',false,true); ?>">返回首页</a></span>
    </div>
    <?php if(\think\App::$debug) { ?>
    <div class="content">
        <div class="info">
            <div class="title">
                <h3>错误位置</h3>
            </div>
            <div class="text">
                <p><?php echo sprintf('in %s', parse_file($file, $line)); ?></p>
            </div>
        </div>
        <div class="info">
            <div class="title">
                <h3>TRACE</h3>
            </div>
            <div class="text">
                    <?php foreach ((array) $trace as $value) { ?>
                    <p>
                        <?php
                        // Show Function
                        if($value['function']){
                            echo sprintf(
                                'at %s%s%s(%s)',
                                isset($value['class']) ? parse_class($value['class']) : '',
                                isset($value['type'])  ? $value['type'] : '',
                                $value['function'],
                                isset($value['args'])?parse_args($value['args']):''
                            );
                        }

                        // Show line
                        if (isset($value['file']) && isset($value['line'])) {
                            echo sprintf(' in %s', parse_file($value['file'], $value['line']));
                        }
                    ?>
                    </p>
                    <?php } ?>
            </div>
            <br />

            <?php if(!empty($source)){?>
            <div class="title" onclick="document.getElementById('source-code').style.display=='none'?document.getElementById('source-code').style.display='block':document.getElementById('source-code').style.display='none'"><h3>Source Code<span>[+]</span></h3></div>
            <div class="text source-code" id="source-code" style="overflow: auto;display: none">
                <pre class="prettyprint lang-php"><ol start="<?php echo $source['first']; ?>"><?php foreach ((array) $source['source'] as $key => $value) { ?><li class="line-<?php echo $key + $source['first']; ?>"><code><?php echo htmlentities($value); ?></code></li><?php } ?></ol></pre>
            </div>
            <br />
            <?php } ?>

            <?php if(!empty($datas)){ ?>
            <div class="title" onclick="document.getElementById('exception-var').style.display=='none'?document.getElementById('exception-var').style.display='block':document.getElementById('exception-var').style.display='none'"><h3>Exception Datas<span>[+]</span></h3></div>
            <div class="text exception-var" id="exception-var" style="overflow: auto;display: none">
                <?php foreach ((array) $datas as $label => $value) { ?>
                <table>
                    <?php if(empty($value)){ ?>
                    <caption><?php echo $label; ?><small>empty</small></caption>
                    <?php } else { ?>
                    <caption><?php echo $label; ?></caption>
                    <tbody>
                    <?php foreach ((array) $value as $key => $val) { ?>
                    <tr>
                        <td><?php echo htmlentities($key); ?></td>
                        <td>
                            <?php
                            if(is_array($val) || is_object($val)){
                                echo htmlentities(json_encode($val, JSON_PRETTY_PRINT));
                            } else if(is_bool($val)) {
                                echo $val ? 'true' : 'false';
                            } else if(is_scalar($val)) {
                                echo htmlentities($val);
                            } else {
                                echo 'Resource';
                            }
                        ?>
                        </td>
                    </tr>
                    <?php } ?>
                    </tbody>
                    <?php } ?>
                </table>
                <?php } ?>
            </div>
            <br />
            <?php } ?>

            <?php if(!empty($tables)){ ?>
            <div class="title" onclick="document.getElementById('exception-var2').style.display=='none'?document.getElementById('exception-var2').style.display='block':document.getElementById('exception-var2').style.display='none'"><h3>Environment Variables<span>[+]</span></h3></div>
            <div class="text exception-var" id="exception-var2" style="overflow: auto;display: none">
                <?php foreach ((array) $tables as $label => $value) { ?>
                <div>
                    <?php if(empty($value)){ ?>
                    <div class="clearfix">
                        <div class="col-md-3"><strong><?php echo $label; ?></strong></div>
                        <div class="col-md-9"><small>empty</small></div>
                    </div>
                    <?php } else { ?>
                    <h3 class="subheading"><?php echo $label; ?></h3>
                    <div>
                        <?php foreach ((array) $value as $key => $val) { ?>
                        <div class="clearfix">
                            <div class="col-md-3"><strong><?php echo htmlentities($key); ?></strong></div>
                            <div class="col-md-9"><small>
                                <?php
                            if(is_array($val) || is_object($val)){
                                echo htmlentities(json_encode($val, JSON_PRETTY_PRINT));
                            } else if(is_bool($val)) {
                                echo $val ? 'true' : 'false';
                            } else if(is_scalar($val)) {
                                echo htmlentities($val);
                            } else {
                                echo 'Resource';
                            }
                        ?>
                            </small></div>
                        </div>
                        <?php } ?>
                    </div>
                    <?php } ?>
                </div>
                <?php } ?>
            </div>
            <br />
            <?php } ?>
        </div>
    </div>
    <script>
        var LINE = <?php echo $line; ?>;

        function $(selector, node){
            var elements;

            node = node || document;
            if(document.querySelectorAll){
                elements = node.querySelectorAll(selector);
            } else {
                switch(selector.substr(0, 1)){
                    case '#':
                        elements = [node.getElementById(selector.substr(1))];
                        break;
                    case '.':
                        if(document.getElementsByClassName){
                            elements = node.getElementsByClassName(selector.substr(1));
                        } else {
                            elements = get_elements_by_class(selector.substr(1), node);
                        }
                        break;
                    default:
                        elements = node.getElementsByTagName();
                }
            }
            return elements;

            function get_elements_by_class(search_class, node, tag) {
                var elements = [], eles,
                        pattern  = new RegExp('(^|\\s)' + search_class + '(\\s|$)');

                node = node || document;
                tag  = tag  || '*';

                eles = node.getElementsByTagName(tag);
                for(var i = 0; i < eles.length; i++) {
                    if(pattern.test(eles[i].className)) {
                        elements.push(eles[i])
                    }
                }

                return elements;
            }
        }

        $.getScript = function(src, func){
            var script = document.createElement('script');

            script.async  = 'async';
            script.src    = src;
            script.onload = func || function(){};

            $('head')[0].appendChild(script);
        }

        ;(function(){
            var files = $('.toggle');
            var ol    = $('ol', $('.prettyprint')[0]);
            var li    = $('li', ol[0]);

            // 短路径和长路径变换
            for(var i = 0; i < files.length; i++){
                files[i].ondblclick = function(){
                    var title = this.title;

                    this.title = this.innerHTML;
                    this.innerHTML = title;
                }
            }

            // 设置出错行
            var err_line = $('.line-' + LINE, ol[0])[0];
            err_line.className = err_line.className + ' line-error';

            $.getScript('//cdn.bootcss.com/prettify/r298/prettify.min.js', function(){
                prettyPrint();

                // 解决Firefox浏览器一个很诡异的问题
                // 当代码高亮后，ol的行号莫名其妙的错位
                // 但是只要刷新li里面的html重新渲染就没有问题了
                if(window.navigator.userAgent.indexOf('Firefox') >= 0){
                    ol[0].innerHTML = ol[0].innerHTML;
                }
            });

        })();
    </script>
    <?php } ?>

</div>
</body>
</html>
